package session.stateless.FRM;

import entity.FRM.CashInAdvance;
import entity.CRM.Customer;
import entity.CRM.SaleOrder;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class CashInAdvanceBean implements CashInAdvanceBeanLocal 
{
    @PersistenceContext 
    private EntityManager em; 
    
    public void createCashInAdvance(Long saleOrderId, Double amount) {
        SaleOrder saleOrder =em.find(SaleOrder.class, saleOrderId);
        
        CashInAdvance cashInAdvance = new CashInAdvance();
        cashInAdvance.setCashInAdvanceAmount(amount);
        
        em.persist(cashInAdvance);
        
        Customer customer =saleOrder.getCustomer();
        //customer.getCashInAdvances().add(cashInAdvance);
        customer.getCreditAccount().getCashInAdvances().add(cashInAdvance);
        customer.getCreditAccount().setCreditBalance(customer.getCreditAccount().getCreditBalance()-amount);
        saleOrder.setCashInAdvance(cashInAdvance);
        
        em.flush();
    }
}
